#include "Thread.hpp"

#include <iostream>



namespace tp{

Thread::Thread()
:_isrunning(false){

}

Thread::~Thread(){}


void Thread::start(){
    if(_isrunning == false){
        pthread_create(&_pthid,NULL,start_routine,(void*)this);
        _isrunning = true;
    }
}

void* Thread::start_routine(void* arg){
    if(arg){
        Thread* pthread = (Thread*)arg;
        pthread->run();
    
    }
    return NULL;
}



void Thread::join(){
    if(_isrunning){
        pthread_join(_pthid,nullptr);
        _isrunning = false;
    }
}

}

#if 0
//测试代码
class mythread:public tp::Thread{
    void run() override{
        std::cout <<"thread is running" << std::endl;
    }
};


int main()
{
    mythread thread;
    thread.start();
    thread.join();
    thread.start();
    thread.join();
    
    return 0;
}

#endif
